/*
 *  $TEAM_NAME Space Trader
 */
package models;

import java.util.Hashtable;
import java.util.Map;
import java.util.Collection;

import java.util.Observable;

/**
 * Shipyard
 * @author twb
 * @version 1.0
 */
public class Shipyard extends Observable {
	/**
	 * field
	 */
	private Map<String, Boolean> ships;
	
	/**
	 * field
	 */
	private TechLevel systemTechLevel;
	
	/**
	 * No arg constructor
	 */
	public Shipyard(){
		this.ships = null;
	}
	
	 /**
     * Constructor - makes a new market with conditions present
     *  in system.
     *  @param ss
     */
    public Shipyard(StarSystem ss) {
        ships = new Hashtable<String, Boolean>();
        
        this.systemTechLevel = ss.getTechLevel();

        for (Ship ship : Ship.getShips()) {
            ships.put(ship.getName(), ship.getAvailable(ss));
        }
    }

    /**
     * buy a ship
     * @param myShip
     * @param newShip
     * @return if you buy the ship
     */
    public boolean canBuy(Ship myShip, Ship newShip) {
        Boolean available = getAvailable(newShip.getName());

        if (!available) {
            return false;
        }
        
        //can't buy a ship you already own
        ships.put(newShip.getName(), false);
        //make your old ship available or not based on min tech level
        ships.put(myShip.getName(), myShip.getAvailableTech(this.systemTechLevel));
        
        setChanged();
        return true;
    }
    
    /**
     * buy
     * @param myShip
     * @return if it goes through
     */
    public boolean canBuy(Ship myShip) {   
        setChanged();
        return true;
    }
   
    /**
     * Get the ship collection
     * @return the collection
     */
    public Collection<Ship> getShipCollection() {
        return Ship.getShips();
    }
    
    /**
     * if the ship is available
     * @param ship
     * @return if it is available
     */
    public Boolean getAvailable(String ship) {
    	return ships.get(ship);
    }

    /**
     * Gets the price
     * @param ship
     * @return the price
     */
    public int getPrice(Ship ship) {
        return ship.getPrice();
    }

    /**
     * Get the system tech level
     * @return the tech level
     */
	public TechLevel getSystemTechLevel() {
		return systemTechLevel;
	}

	/**
	 * Set the system tech level
	 * @param systemTechLevel 
	 */
	public void setSystemTechLevel(TechLevel systemTechLevel) {
		this.systemTechLevel = systemTechLevel;
	}
	
	/**
	 * get the ships
	 * @return the map of ships
	 */
	public Map<String, Boolean> getShips() {
		return this.ships;
	}

	/**
	 * Sets the ships
	 * @param ships
	 */
	public void setShips(Map<String, Boolean> ships) {
		this.ships = ships;
	}
	
	/**
	 * to string
	 * @return string
	 */
	public String toString() {
		return "";
	}
}